package softtech.apps.autoanswerphonecall;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Build;
import softtech.apps.autoanswerphonecall.R;

public class MessageDetailActivity extends Activity {

	EditText mEdit;
	int message_id;
	String message_content;
	int message_is_choose;

	DatabaseHandler db;
	Context context = this;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_message_detail);
		
		db = new DatabaseHandler(context);
		
		 // gets the activity's default ActionBar
        android.app.ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#2ac2d4"))); 
        actionBar.show();

        
		// actionBar.setDisplayHomeAsUpEnabled(true);
		 //getSupportActionBar().setTitle("test");
        

        Bundle extras = getIntent().getExtras();
        message_content = extras.getString("message_content");
        message_id = extras.getInt("message_id");
        message_is_choose =  extras.getInt("message_is_choose");
        
        
        mEdit   = (EditText)findViewById(R.id.editMessageDetail);
    	mEdit.setText(message_content);
    	
    	
    	Button btnSave = (Button)findViewById(R.id.btnSave);
    	btnSave.setOnClickListener(new OnClickListener() {
		
   
			@Override
			public void onClick(View v) {
				// Update to database
				
			Message mes = new Message(message_id, mEdit.getText().toString(), message_is_choose);
			
			int x = db.updateMessage(mes);
//			Log.d("fuck","Update status = "+x);
			db.getAllMessages();
			
				 Intent toManagerActivity = new Intent(getBaseContext(), MessageManagerActivity.class);
				 toManagerActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				 getBaseContext().startActivity(toManagerActivity);
				 finish();
			}
		});
    	
    	
    	Button btnCancel = (Button)findViewById(R.id.btnCancel);
    	btnCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent toManagerActivity = new Intent(getBaseContext(), MessageManagerActivity.class);
				 toManagerActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				 getBaseContext().startActivity(toManagerActivity);
				 finish();
			}
		});
	}

	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.message_detail, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		switch (item.getItemId()) {
		case android.R.id.home:
	        NavUtils.navigateUpFromSameTask(this);
	        return true;
		default:
	}
		return super.onOptionsItemSelected(item);
	}

}
